package com.winbomb.kingcard.screens;

import java.util.Arrays;
import java.util.Comparator;

import com.winbomb.kingcard.Assets;
import com.winbomb.kingcard.IPlayer;
import com.winbomb.kingcard.IStatsInfo;
import com.winbomb.kingcard.base.GameLogic;
import com.winbomb.kingcard.base.StatsInfo;
import com.winbomb.kingcard.framework.Game;
import com.winbomb.kingcard.framework.Screen;
import com.winbomb.kingcard.framework.Input.TouchEvent;
import com.winbomb.kingcard.framework.impl.TouchArea;
import com.winbomb.kingcard.screens.util.Render;

/**
 * 战斗统计信息界面
 * 
 * @author winbomb
 * 
 */
public class StatsInfoScreen extends Screen {

	private static final TouchArea btnRestartGame = new TouchArea(158, 270, 76, 30);
	private static final TouchArea btnBackToMenu = new TouchArea(250, 270, 76, 30);

	private GameLogic gameImpl;
	private IPlayer[] players;

	public StatsInfoScreen(Game game, GameLogic gameImpl) {
		super(game);
		this.gameImpl = gameImpl;

		// 绘制统计信息
		players = gameImpl.getPlayers();

		// 按照得分进行排序
		Arrays.sort(players, new ScoreComparator());
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

	@Override
	public void pause() {
		Assets.bg_music2.pause();
	}

	@Override
	public void present(float deltaTime) {

		// 绘制背景
		g.drawPixmap(Assets.stats_bg, 0, 0);

		// 游戏胜者
		g.drawText(gameImpl.getWinner().getPlayerName(), 112, 54, Render.WHITE_PAINT);

		// 游戏耗时
		float gameTime = gameImpl.getTimeCost();
		StringBuffer sb = new StringBuffer();
		int min = (int) (gameTime / 60);
		int sec = ((int) gameTime) % 60;
		if (min < 10) {
			sb.append("0");
		}
		sb.append(min);
		sb.append("分");
		if (sec < 10) {
			sb.append("0");
		}
		sb.append(sec);
		sb.append("秒");
		g.drawText(sb.toString(), 380, 54, Render.WHITE_PAINT);

		IStatsInfo statsInfo;
		for (int i = 0; i < players.length; i++) {
			int baseLine = 94 + 15 * i;

			if (players[i].getPlayerIndex() == 0) {
				g.drawPixmap(Assets.stats_elems, 36, baseLine - 13);
			}

			g.drawText(players[i].getPlayerName(), 41, baseLine, Render.WHITE_PAINT);

			statsInfo = players[i].getStatsInfo();
			// 灭敌次数
			g.drawText(statsInfo.getStats(StatsInfo.ST_CONQUER), 130, baseLine, Render.WHITE_PAINT);

			// 被灭次数
			g.drawText(statsInfo.getStats(StatsInfo.ST_BE_CONQUERED), 175, baseLine, Render.WHITE_PAINT);

			// 复活次数
			g.drawText(statsInfo.getStats(StatsInfo.ST_BE_REVITALIZED), 220, baseLine, Render.WHITE_PAINT);

			// 战斗次数
			int battleCnt = statsInfo.getStats(StatsInfo.ST_BATTLE);
			g.drawText(battleCnt, 265, baseLine, Render.WHITE_PAINT);

			// 胜利次数
			int victoryCnt = statsInfo.getStats(StatsInfo.ST_VICTORY);
			g.drawText(victoryCnt, 310, baseLine, Render.WHITE_PAINT);

			// 歼敌数
			g.drawText(statsInfo.getStats(StatsInfo.ST_KILL_COUNT), 355, baseLine, Render.WHITE_PAINT);

			// 功勋值
			g.drawText(statsInfo.getScore(), 400, baseLine, Render.WHITE_PAINT);

		}
	}

	@Override
	public void resume() {
		Assets.bg_music2.play();
	}

	@Override
	public void update(float deltaTime) {
		// 等待人工处理的事件
		updateTouchEvents();

		for (TouchEvent event : touchEvents) {
			if (event.type != TouchEvent.TOUCH_UP) {
				continue;
			}

			if (btnRestartGame.isBound(event)) {
				gameImpl.start();
				game.setScreen(new GameScreen(game, gameImpl));

			} else if (btnBackToMenu.isBound(event)) {
				game.setScreen(new MainScreen(game));
			}
		}

	}

	private static class ScoreComparator implements Comparator<IPlayer> {

		@Override
		public int compare(IPlayer object1, IPlayer object2) {

			if (object1 == object2) {
				return 0;
			}

			if (object1 != null && object2 != null) {
				int score1 = object1.getStatsInfo().getScore();
				int score2 = object2.getStatsInfo().getScore();

				return score2 - score1;
			}

			return 0;
		}
	}

}
